package com.shd.edanpei.presenter;

import com.shd.edanpei.ui.activity.WithdrawActivity;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.WithdrawModel;
import com.shd.edanpei.presenter.contract.WithdrawalContract;
import com.shd.edanpei.db.DaoManager;

import java.util.WeakHashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/4/4.
 * yang-wen-wen
 */
public class WithdrawalPresenter extends BasePresenterImpl<WithdrawalContract.WithdrawalIView>
        implements WithdrawalContract.WithdrawalIPresenter {

    public WithdrawalPresenter(WithdrawalContract.WithdrawalIView mView) {
        super(mView);
    }

    @Override
    public void submit(final Constant.withdraw withdraw, final String amount, final String userName,
                       final String account, final String bank, final String openBank) {
        WeakHashMap<String, Object> hashMap = new WeakHashMap<>();
        hashMap.put("userId", DaoManager.getInstance().getUserBean().getId());
        hashMap.put("accountName", userName);
        hashMap.put("accountNum", account);
        hashMap.put("type", withdraw.getCode());
        hashMap.put("amountOfCash", amount);
        if (withdraw.getCode() == Constant.withdraw.bank.getCode()) {
            hashMap.put("bankName", bank);
            hashMap.put("openBankName", openBank);
        }

        ApiCreator.getApi().withdraw(hashMap)
                .compose(RxTransformer.<WithdrawModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<WithdrawModel>() {
                    @Override
                    protected void onSuccess(WithdrawModel data) {
                        switch (data.getCode()) {
                            case 0://您的提现申请已提交，请等待
                                DialogUtil.showSuccessDialog(mBaseIView.getContext(), new DialogUtil.SureListener() {
                                    @Override
                                    public void sure() {
                                        WithdrawActivity.instance.finish();
                                        mBaseIView.getContext().finish();
                                    }
                                }, "提现成功！", "本次提现金额将在1-7个工作日发放到您到账户，请耐心等待");
                                break;
                            case 1://只允许当月20号之前进行提现
                                DialogUtil.showDialog4(mBaseIView.getContext(), new DialogUtil.SureListener() {
                                    @Override
                                    public void sure() {
                                        mBaseIView.getContext().finish();
                                    }
                                }, "温馨提示", "本次提现操作已超出了规定时间范围，请详细查看\"提现须知\"，谢谢合作！", "确定");
                                break;
                            case 2://积分不足，无法提现
                                DialogUtil.showDialog4(mBaseIView.getContext(), new DialogUtil.SureListener() {
                                    @Override
                                    public void sure() {
                                        mBaseIView.getContext().finish();
                                    }
                                }, "温馨提示", "您账户的积分额度不足，不可提现，请详细查看\"提现须知\"，谢谢合作！", "确定");
                                break;
                            default:
                                break;
                        }
                    }
                });
    }
}
